{extend name="public:base"/}


{block name="title"}新增权限{/block}
{block name="style"}
<link rel="stylesheet" href="__STATIC__/admin/plugin/zTree/zTreeStyle.css" type="text/css">
{/block}

{block name="main"}

<section class="content">
    <div class="row">

        <div class="col-md-12">

            <div class="row">
                <div class="col-lg-12">
                    <div class="box">
                        <div class="box-header with-border">
                            <h3 class="box-title">编辑角色</h3>
                        </div>
                        <form  id="pt_form" method="post">
                            <div class="box-body">

                                <div class="clearfix"></div>

                                <input type="hidden" name="id" value="<?=$role['id']?>">
                                <div class="form-group required">
                                    <div class='col-sm-2 text-right'>
                                        <label class="control-label Validform_label" for="role_name">角色名称</label>
                                    </div>
                                    <div class='col-sm-10'>
                                        <input type="text" id="role_name" class="form-control" name="role_name" placeholder="角色名称" value="<?=$role['role_name']?>" datatype="*">
                                        <div class="help-block"></div>
                                    </div>
                                </div>

                                <div class="form-group required">
                                    <div class='col-sm-2 text-right'><label class="control-label">是否启用</label></div>
                                    <div class='col-sm-10'>
                                        <input type="hidden" name="Article[status]" value="">
                                        <div id="article-status" role="radiogroup">
                                            <label>
                                                <input type="radio" name="status" value="1" <?=$role->status==1?'checked="checked"':''?>>
                                                启用
                                            </label>

                                            <label>
                                                <input type="radio" name="status" value="0" <?=$role->status==0?'checked="checked"':''?>>
                                                禁用
                                            </label>
                                        </div>
                                        <div class="help-block"></div>
                                    </div>
                                </div>

                                <div class="form-group required">
                                    <div class='col-sm-2 text-right'>
                                        <label class="control-label">选择权限</label>
                                        <input type="hidden" id="privilege_id" name="privilege_id"/>
                                    </div>
                                    <div class='col-sm-10'>
                                        <div class="">&nbsp;</div>
                                        <div id="treeType" class="ztree"></div>
                                        <div class="help-block"></div>
                                    </div>

                                </div>

                            </div>
                            <div class="box-footer text-center">
                                <button class="btn btn-primary" type="submit">保存</button>
                                <span class="btn btn-white" onclick="history.go(-1)">返回</span>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
{/block}

{block name="js"}
<script src="__STATIC__/admin/plugin/zTree/jquery.ztree.core-3.5.js"></script>
<script src="__STATIC__/admin/plugin/zTree/jquery.ztree.excheck-3.5.js"></script>
<script src="__STATIC__/admin/plugin/zTree/jquery.ztree.exedit-3.5.js"></script>

<script>
    var zTree;
    var privilege = JSON.parse('[<?=$role->privilege_id?>]');
    jQuery(function ($) {
        var zNodes = {:json_encode($privilegeData)};
        // 设置zetree
        var setting = {
            check:{
                enable:true
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "parent_id",
                }
            },
            callback:{
                onNodeCreated:getNodes
            }
        };
        $.fn.zTree.init($("#treeType"), setting, zNodes);
        zTree = $.fn.zTree.getZTreeObj("treeType");
    });

    function getNodes(event, treeId, treeNode){
        zTree = $.fn.zTree.getZTreeObj("treeType");
        zTree.expandNode(treeNode,true, true, true);
        if($.inArray(treeNode.id,privilege)>-1){
            zTree.checkNode(treeNode,true,false);
        }
    }

    var validate = $("#pt_form").Validform({
        tiptype: function(msg,o,cssctl){
            if(o.type == 3){
                toastr.error(msg);
                return false;
            }
        },
        datatype:{
        },
        tipSweep:true, //是否失去焦点就验证
        postonce:true, //表单只提交一次
        ajaxPost:true, //异步提交表单
        beforeSubmit:function(curform){
            var zTree = $.fn.zTree.getZTreeObj("treeType");
            var nodes = zTree.getCheckedNodes(true);
            var arr = [];
            $.each(nodes, function (n, value) {
                if(value.id > 0){
                    arr.push(value.id);
                }
            });
            arr.sort();
            if(arr.length < 1){
                toastr.error('权限至少选择一项');
                return false;
            }
            $('#privilege_id').val(arr);
            layer.load(2);
        },
        callback:function(data){
            if(data.code==300){
                layer.closeAll();
                toastr.error(data.msg);
                return false;
            }else{
                window.location.href= "<?=url('@admin/role/index')?>";
                return false;
            }
        }
    });

</script>
{/block}